<template>
  <div class='container'>
    <el-card style="margin:30px 20px" shadow="never">
      <!-- 搜索区 -->
      <el-form label-width="65px">
          <el-row :gutter="20" type="flex">
              <!-- 关键字 -->
            <el-col :span="7">
              <el-form-item label="关键字">
                <el-input placeholder="根据文章标题搜索" v-model="artObj.keyword" ></el-input>
              </el-form-item>
            </el-col>
               <!-- 状态 -->
            <el-col :span="5">
              <el-form-item label="状态">
                <el-select v-model="artObj.state" >
                  <el-option label="已启用" :value="1"></el-option>
                  <el-option label="已禁用" :value="0">  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
              <!-- 按钮 -->
            <el-col :span="5">
              <el-form-item>
                <el-button @click="clear">清除</el-button>
                <el-button type="primary" @click="search">搜索</el-button>
              </el-form-item>
            </el-col>
              <!-- 新增 -->
            <el-col :span="7">
              <el-form-item>
                <el-button
                @click="addArticles"
                type="success"
                icon="el-icon-edit">新增技巧
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
      </el-form>
      <!-- 提示框 -->
      <el-alert :title='title' type="info" show-icon></el-alert>
      <!-- 数据列表 -->
      <el-table :data="dataList">
         <el-table-column label="序号" width="100px">
          <template #default="{$index}">
            {{$index+1}}
          </template>
         </el-table-column>
         <el-table-column label="文章标题" width="300px" prop="title">
          <template #default="{row}">
            {{row.title}}
            <span v-if="row.videoURL" class="el-icon-film" @click="videoFn(row.videoURL)"></span>
          </template>
         </el-table-column>
         <el-table-column label="阅读数" prop="visits" ></el-table-column>
         <el-table-column label="录入人" prop="username" ></el-table-column>
         <el-table-column label="录入时间" prop="createTime" width="200px"></el-table-column>
         <el-table-column label="状态"  width="150px" prop="state">
          <template #default="{row}">
            <!-- 默认row.state=true=1 -->
            {{row.state?'已启用':"已禁用"}}
          </template>
         </el-table-column>
         <el-table-column label="操作" width="250px">
          <template #default="{row}">
            <el-button type="text" @click="lookArticles(row.id)" >预览</el-button>
            <!-- <el-button type="text" v-if="state=0" @click="fnArticles(row.id)" >禁用</el-button> -->
            <el-button type="text" @click="stateArticles(row.id)">{{row.state?'禁用':'启用'}}</el-button>
            <el-button type="text" :disabled='row.state?true:false' @click="updateArticles(row.id)">修改</el-button>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delArticles(row.id)">
              <el-button slot="reference" type="text" :disabled='row.state?true:false'>删除</el-button>
            </el-popconfirm>
            <!-- <el-button type="text" @click="delArticles(row.id)" :disabled='row.state?true:false'>删除</el-button> -->
          </template>
         </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="end" align="middle">
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange($event)"
      :current-page="artObj.page"
      :page-size="artObj.pagesize"
      :page-sizes="[2,5,10]"
      :total="total"
      layout=" prev, pager, next, sizes,jumper"
      >
    </el-pagination>
      </el-row>
    </el-card>
    <!-- 添加文本框 -->
    <el-dialog :title="titles" :visible.sync="showArticles" @close="closeArticlesForm">
      <el-form label-width="100px" :model="form" :rules="rules" ref="form">
        <!-- 第一层 -->
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <!-- 第二层 -->
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor :options="editorOption" v-model="form.articleBody" @blur="$refs.form.validateField('articleBody')" ></quill-editor>
        </el-form-item>
        <!-- 第三层 -->
        <el-form-item label="视频地址" prop="videoURL">
          <el-input v-model="form.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
        <!-- 第四层 -->
        <el-row type="flex" justify="center">
          <el-button @click="closeArticlesForm">取消</el-button>
          <el-button type="primary" @click="addArticlesForm">确认</el-button>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- 添加预览文本框 -->
    <el-dialog title="预览文章" :visible.sync="showArticlesForm">
      <el-container>
        <el-header>
          <p>{{showForm.title}}</p>
          <p>
            <span>{{showForm.createTime}}</span>
            <span>{{showForm.username}}</span>
            <span class="el-icon-view">{{showForm.visits}}</span>
          </p>
        </el-header>
        <el-main >
          <div v-html="showForm.articleBody"></div>
        </el-main>
      </el-container>
    </el-dialog>
    <!-- 视频框 -->
    <div class="box" v-if="videoShow">
      <div class="close" @click="closeVideo">
        <span class="el-icon-close"></span>
      </div>
      <div class="video">
        <video :src="videoUrl" class="video1" autoplay></video>
      </div>
    </div>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { simple, list, add, remove, update } from '@/api/hmmm/articles.js'

export default {
  components: {
    quillEditor
  },
  data () {
    return {
      editorOption: { // 配置工具栏
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            [{ list: 'ordered' }, { list: 'bullet' }], // 有序列表，无序列表
            ['blockquote', 'code-block'], // 引用，代码块
            ['image'],
            ['link']
          ]
        }
      },
      videoUrl: '',
      fn: '',
      titles: '',
      showArticlesForm: false,
      videoShow: false,
      showForm: '',
      title: '',
      total: 0,
      dataList: [],
      artObj: {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null

      },
      showArticles: false,
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async list () {
      await simple(this.artObj)
      // console.log(res)
    },
    async articlesData () {
      const res = await list(this.artObj)
      this.dataList = res.data.items
      // console.log(555, this.dataList)
      this.title = `数据一共有${res.data.counts}条`
      this.total = res.data.counts
    },
    handleSizeChange (val) {
      this.artObj.page = 1
      // console.log(this.page)
      this.artObj.pagesize = val
      this.articlesData()
    },
    handleCurrentChange (val) {
      this.artObj.page = val
      this.articlesData()
    },
    // 禁止/删除按钮切换
    stateArticles (id) {
      const state = this.dataList.find(ele => ele.id === id).state // 筛选出具体某一行的state
      this.dataList.find(ele => ele.id === id).state = !state // 取反
      this.$message.success('操作成功')
    },
    // 打开文本框
    addArticles () {
      this.showArticles = true
      this.titles = '添加文章'
    },
    // 添加
    addArticlesForm () {
      this.$refs.form.validate().then(async () => {
        if (this.titles === '添加文章') {
          // console.log(111)
          await add(this.form)
          this.$message.success('添加成功')
          this.articlesData()
          this.closeArticlesForm()
        } else {
          await update(this.form)
          // console.log(111)
          this.$message.success('修改成功')
          this.articlesData()
          this.closeArticlesForm()
        }
      })
    },
    // 关闭文本框
    closeArticlesForm () {
      this.$refs.form.resetFields()
      this.showArticles = false
    },
    // 删除
    async delArticles (id) {
      await remove({ id })
      this.articlesData()
      this.$message.success('删除成功')
    },
    // 预览
    async lookArticles (id) {
      this.showArticlesForm = true
      // const res = await detail({ id })
      // console.log(res)
      // this.dataList.push(res.data)
      // this.dataList = res.data
      const res = this.dataList.filter(item => item.id === id)
      // console.log(res)
      this.showForm = res[0]
      // console.log(this.showForm)
    },
    // 修改
    async updateArticles (id) {
      this.showArticles = true
      this.titles = '修改文章'
      const res = this.dataList.filter(item => item.id === id)
      this.form = res[0]
      // console.log(this.form)
      // this.articlesData()
      // console.log(this.id)
      // const res = await update(id)
      // console.log(res)
    },
    videoFn (val) {
      this.videoShow = true
      this.videoUrl = val
    },
    // 关键字搜索
    search () {
      this.articlesData()
    },
    // 清理
    clear () {
      this.artObj.state = null
      this.artObj.keyword = null
    },
    closeVideo () {
      this.videoShow = false
    }
  },
  created () {
    this.list()
    this.articlesData()
    // this.lookArticles()
  }
  // computed:{
  //   title(){
  //     return this.
  //   }
  // }
}
</script>

<style scoped>
  .el-header, .el-footer {
    margin-right: 10px;
  }
  .el-header span{
     margin-left: 10px;
  }
  .el-aside {
    background-color: #D3DCE6;
    /* color: #333; */
    /* text-align: center; */
    /* line-height: 200px; */
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    /* text-align: center; */
    /* line-height: 160px; */
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
.box{
  position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,.3);
    overflow: hidden;
    z-index: 9999;
}
.close{
  width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%);
    background: rgba(0,0,0,.4);
    box-shadow: 0 0 5px rgba(0,0,0,.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
    z-index: 999;
}
.video{
      width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}
.video1{
    width: 100%;
    height: 100%;
}
.articles-add[data-v-292d3822] .ql-editor {
    height: 200px;
}
.ql-editor {
    box-sizing: border-box;
    line-height: 1.42;
    height: 200px;
    outline: none;
    overflow-y: auto;
    padding: 12px 15px;
    -o-tab-size: 4;
    tab-size: 4;
    -moz-tab-size: 4;
    text-align: left;
    white-space: pre-wrap;
    word-wrap: break-word;
}

</style>
